//
//  PlottingScaleView.m
//  sdk2.0zhengquandasha
//
//  Created by Choi on 15/12/28.
//  Copyright © 2015年 palmaplus. All rights reserved.
//

#import "PlottingScaleView.h"

@interface PlottingScaleView ()
@property (nonatomic,strong)UIView *plottingView;
@property (nonatomic,strong)UILabel *plottingTitle;
@property (nonatomic,assign)CGFloat firstPlottingDis;
@property (nonatomic,strong)UIView *headView;
@property (nonatomic,strong)UIView *footView;
@property (nonatomic,assign)BOOL isFirstPlottingDis;
@end

@implementation PlottingScaleView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initAll];
        self.changeAbled = YES;
    }
    return self;
}

-(void)initAll{
    self.isFirstPlottingDis = YES;
    CGFloat tempHeight = 5;
    CGSize size = self.bounds.size;
    self.plottingTitle = [[UILabel alloc]initWithFrame:CGRectMake(-3, 0, 55, size.height/2)];
    self.plottingTitle.textColor = [UIColor blackColor];
    self.plottingTitle.textAlignment = NSTextAlignmentCenter;
    self.plottingTitle.font = [UIFont systemFontOfSize:10];
    [self addSubview:self.plottingTitle];
    
    self.plottingView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.plottingTitle.frame), 55,1.5)];
    self.plottingView.backgroundColor = [UIColor blackColor];
    [self addSubview:self.plottingView];
    
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.plottingTitle.frame) - tempHeight, 1.5, tempHeight)];
    self.headView.backgroundColor = [UIColor blackColor];
    [self addSubview:self.headView];
    
    self.footView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.plottingView.frame) - 1.5, CGRectGetMaxY(self.plottingTitle.frame) - tempHeight, 1.5, tempHeight)];
    self.footView.backgroundColor = [UIColor blackColor];
    [self addSubview:self.footView];
    
}

-(void)setScaleNum:(int)scaleNum{
    self.plottingTitle.text = [NSString stringWithFormat:@" %d米",scaleNum];
    _scaleNum = scaleNum;
}

-(void)setPlottingDistance:(CGFloat)plottingDistance{
    if (!self.changeAbled) {
        return;
    }
    
    CGRect rect = self.plottingView.frame;
    _plottingDistance = plottingDistance;
    if (self.isFirstPlottingDis) {
        _firstPlottingDis = plottingDistance;
        self.isFirstPlottingDis = NO;
    }
    
    if (plottingDistance <= _firstPlottingDis) {
        self.plottingView.frame = CGRectMake(0, rect.origin.y, plottingDistance, rect.size.height);
        self.plottingTitle.text = [NSString stringWithFormat:@" %d米",self.scaleNum];
    }else{
        int temp = plottingDistance/_firstPlottingDis;
        
        CGFloat tempF = plottingDistance - (temp - 1) *_firstPlottingDis ;
        //CGFloat tempF = plottingDistance ;
        int newTemp = _scaleNum/temp;
        if (newTemp < 1) {
            newTemp = 1;
        }
        self.plottingTitle.text = [NSString stringWithFormat:@" %d米",newTemp];
        
        self.plottingView.frame = CGRectMake(0, rect.origin.y, tempF, rect.size.height);
    }
    CGFloat w = self.plottingView.width;
    if (self.plottingView.width < 30) {
        w = 30;
    }
    self.plottingTitle.width = w;
    CGRect footRect = self.footView.frame;
    self.footView.frame = CGRectMake(CGRectGetMaxX(self.plottingView.frame)- rect.size.height, footRect.origin.y, footRect.size.width, footRect.size.height);

}

@end
